# /usr/bin/python
# -*- coding: utf-8 -*-

import requests
import json

class ZBX(object):
    _url = "http://zabbix.juanpi.org/api_jsonrpc.php"
    _user = "jingqiu"
    _password = "97aysd*&%SjkhgI&TSD*(^YSGHDJFSH"
    _headers = {"Content-Type": "application/json-rpc"}

    def __init__(self):
        self.token = self.login()

    def login(self):
        """
        默认的用户登录，获取操作类所需的token
        """
        url = self._url
        headers = {"Content-Type": "application/json-rpc"}  
        login_data = {
            "jsonrpc": "2.0",
            "method" : "user.login",
            "params": {
                "user": self._user,
                "password": self._password
            },
            "id": 0,
            "auth": None
        }

        req = requests.post(self._url, data=json.dumps(login_data),headers=self._headers)
        res = json.loads(req.text)
        token = res['result']
        return token

    def callback(self,method,params):
        """
        Zabbix操作类的回调基础方法
        """
        post_data = {
            "jsonrpc": "2.0",
            "method": method,
            "params": params,
            "id": 1,
            "auth": str(self.token)
        }

        #print json.dumps(post_data)
        req = requests.post(self._url, data=json.dumps(post_data), headers=self._headers)
        try:
            res = json.loads(req.text)
            return res["result"]
        except ValueError as e:
            return None

    def get_host(self,*args,**kwargs):
        """
        :param      host            list    过滤包含主机名称列表
        :param      name            list    过滤包含主机名称列表
        :param      proxy_hostid    list    过滤主机代理ID列表
        :param      hostid          list    过滤主机ID列表    
        """
        method = "host.get"
        host = {
            "output": ["available","host","name","proxy_hostid","hostid"],
            "selectGroups": ["groupid","name"],
            "selectParentTemplates": ["name"],
            "filter": kwargs
        }

        for arg in args:
            host.update(arg)

        res = self.callback(method,host)
        return res

    def get_group(self,**kwargs):
        """
        :param      name        list    包含的主机组名称        
        """
        method = "hostgroup.get"
        group = {
            "output": ["name","groupid"],
            "filter": kwargs
        }

        res = self.callback(method,group)
        return res

    def get_template(self,**kwargs):
        """
        :param      host        list        主机名称列表
        """
        method = "template.get"
        template = {
            "output": ["host","name","templateid"],
            "selectGroups": ["name"],
            "selectHosts": ["hostid","name"],
            "selectParentTemplates": ["name"],
            "filter": kwargs
        }

        res = self.callback(method,template)
        return res
    
    def get_interface(self,**kwargs):
        """
        :param      hostids     list        所属主机的ID列表
        """
        method = "hostinterface.get"
        interface = {
            "output": "extend",
            "selectHosts": ["name"]
        }
        interface.update(kwargs)
        
        res = self.callback(method,interface)
        return res 
    
    def get_trigger(self,**kwargs):
        """
        :param      templateids     list    模板ID列表
        :param      groupids        list    所属主机ID组列表
        :param      hostids         list    连接主机ID列表
        :param      itemids         list    包含itemID列表
        :param      triggerids      list    包含triggerID列表
        """
        method = "trigger.get"
        trigger = {
            "output": ["function","triggerid","expression","description","templateid"],
            "selectFunctions": "extend"
        }
        trigger.update(kwargs)
        
        res = self.callback(method,trigger)
        return res

    def get_item(self,**kwargs):
        """
        :param      templateids     list        所属模板ID列表
        :param      groupids        list        所属主机组列表
        :param      hostids         list        连接主机ID列表
        :param      itemids         list        包含itemID列表
        :param      triggerids      list        包含triggerID列表
        :param      host            string      所属主机
        :param      group           string      所属主机组
        :param      interfaceids    list        所属interfaceID列表
        """
        method = "item.get"
        items = {
            "output": ["itemid","hostid","key_",]
        }
        items.update(kwargs)

        res = self.callback(method,items)
        return res
